<?php
/**
 * User: krisbuist
 * Date: 1/19/12
 * Time: 3:14 PM
 */

require_once APPPATH . 'core/controller.php';

class UserController extends Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->data['styles'][] = "/public/css/frontpage-index.css";
    }

    public function view($userName)
    {
        $user = $this->em->getRepository('User')->findOneBy(array('username' => $userName));
        if ($user == null) {
            show_404();
        }

        $userId = $user->getId();
        $this->data['theUser'] = $user;

        $this->data['genders'] = array('m' => 'zijn', 'v' => 'haar', 'o' => 'zijn/haar');



        $usersLatestPosts = $this->em->createQuery("SELECT p FROM Post p JOIN p.author a WHERE a.id = :userId ORDER BY p.id DESC")->setMaxResults(6);
        $usersLatestPosts->setParameter('userId', $userId);
        $this->data['usersLatestPosts'] = $usersLatestPosts->getResult();

        $usersLatestComments = $this->em->createQuery("SELECT c FROM Comment c JOIN c.author a WHERE a.id = :userId ORDER BY c.id DESC")->setMaxResults(6);
        $usersLatestComments->setParameter('userId', $userId);
        $this->data['usersLatestComments'] = $usersLatestComments->getResult();

        $usersCategorySubscriptions = $user->getCategorySubscriptions();
        $usersChannelSubscriptions = $user->getChannelSubscriptions();
        $this->data['usersCategorySubscriptions'] = $usersCategorySubscriptions;
        $this->data['usersChannelSubscriptions'] = $usersChannelSubscriptions;

        $usersPostRatings = $this->em->createQuery("SELECT SUM(r.rating) FROM Post_Rating r JOIN r.user u WHERE u.id = :userId");
        $usersPostRatings->setParameter('userId', $userId);
        $this->data['usersPostRatings'] = $usersPostRatings->getSingleScalarResult();

        $usersCommentRating = $this->em->createQuery("SELECT SUM(r.rating) FROM Comment_Rating r JOIN r.user u WHERE u.id = :userId");
        $usersCommentRating->setParameter('userId', $userId);
        $this->data['usersCommentRatings'] = $usersCommentRating->getSingleScalarResult();

        $this->parser->parse('user/view.tpl', $this->data);
    }
}
